<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      globalAudioContext: null,
      time:0,
    };
  },
  onShow(){
    this.login();
  },
  onHide(){
    this.setLoginReady(false);
  },
  onLaunch() {
    this.updateSet();
  },
  created() {
    console.log("码云git版本v1.1.3");
    this.getDevice(); // 获取设备信息
    this.checkAuthorize(); //获取授权状态
  },
  mounted() {
    this.initAudio();
  },
  computed: {
    ...mapState(["appInfo"])
  },
  methods: {
    ...mapMutations([
      "setGlobalAudio",
      "setRankList",
      "setMyRank",
      "setLoginReady",
      "setAuthorize",
      "setInitInfo"
    ]),
    initAudio() {
      this.globalAudioContext = wx.createInnerAudioContext();
      this.globalAudioContext.src = "static/audio/bgm.mp3";
      this.globalAudioContext.loop = true;
      this.globalAudioContext.volume = 1;
      this.globalAudioContext.play();
      this.setGlobalAudio(this.globalAudioContext);
    },
    login() {
      // 微信登录
      wx.login({
        success: res => {
          let code = res.code;
          // 登录
          wx.showLoading({
            title: "登录中,请稍后",
            mask: true
          });
          this.fly
            .get("/login", {
              code: code
            })
            .then(res => {
              if (res.data.Tag == 1) {
                // 用户信息初始化
                this.store.commit("setAppInfo", res.data.Result); // 全局管理SesionId
                this.store.commit("setSessionId", res.data.SessionId); // 全局管理SesionId
                // this.init();
                this.getConfig();
                this.getInitInfo();
                // if(this.time ==0){
                this.checkFriend();
                // }
                // this.time++;
                this.setLoginReady(true);
                wx.hideLoading();
              }
            });
        },
        fail: res => {}
      });
    },
    updateSet() {
      //通用app更新 提示 更新
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function(res) {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate);
      });
      updateManager.onUpdateReady(function() {
        wx.showModal({
          title: "更新提示",
          content: "新版本已经准备好，是否重启应用？",
          success: function(res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate();
            }
          }
        });
      });

      updateManager.onUpdateFailed(function() {
        // 新的版本下载失败
        wx.showModal({
          title: "更新提示",
          content: "新版本下载失败",
          showCancel: false
        });
      });
    },
    getDevice() {
      //获取设备信息
      wx.getSystemInfo({
        success: res => {
          this.store.commit("setDeviceInfo", {
            width: res.windowWidth,
            height: res.windowHeight
          });
        }
      });
    },
    getConfig() {
      //获取系统基础配置信息
      this.fly.post("/getDataConfig").then(res => {
        let obj = {},
          key = {
            intervalTime: "baseTime",
            smallCardNumber: "baseCard",
            unLockStrengthCount: "baseEnergy",
            moneyToStrengthFull: "baseEnergyCost",
            moneyToJumpSubject: "baseSkipCost",
            shareToAddStrength: "baseShareGetEnergy",
            maxMoney: "maxEnergy"
          };
        for (let i = 0; i < res.data.Result.length; i++) {
          obj[key[res.data.Result[i].name]] = res.data.Result[i].value;
        }
        console.log(obj);
        this.store.commit("setSysConfig", obj);
      });
    },
    getInitInfo(){
      this.fly.post('/init').then(res=>{
        this.setInitInfo(res.data);
      })
    },
    checkFriend() {
      this.fly.post("/getRankList").then(res => {
        let myRank;
        if (res.data.Result.length >= 0) {
          // 筛选个人信息
          for (let i = 0; i < res.data.Result.length; i++) {
            if (res.data.Result[i].userId == this.appInfo.userId) {
              myRank = res.data.Result[i];
              this.setMyRank(res.data.Result[i]);
            }
          }
          // res.data.Result.forEach(item => {
          //   if (item.userId == this.appInfo.userId) {
          //     this.myRank = item;
          //   }
          // });
          //获取超越我的好友列表
          let list = [];
          for (let i = 0; i < res.data.Result.length; i++) {
            if (res.data.Result[i].cardNumber > myRank.cardNumber) {
              list.push(res.data.Result[i]);
            }
          }
          this.setRankList(list);
          //渲染列表 和弹框
        }
      });
    },
    checkAuthorize() {
      //检查授权状态
      wx.getStorage({
        key: "isAuthorize",
        success: res => {
          this.store.commit("setAuthorize", res.data);
        },
        fail: () => {
          wx.setStorage({
            key: "isAuthorize",
            data: "0"
          });
          this.store.commit("setAuthorize", 0);
        }
      });
    }
  }
};
</script>

<style>
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
  font-family: "Helvetica Neue", Helvetica, Arial, "PingFang SC",
    "Hiragino Sans GB", "Heiti SC", "Microsoft YaHei", "WenQuanYi Micro Hei";
}
page {
  height: 100%;
}
</style>
